package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
import com.hamster.crm.mapper.ProjectCategoryMapper;
import com.hamster.crm.mapper.ProjectCourseMapper;
import com.hamster.crm.mapper.ProjectMapper;
import com.hamster.crm.pojo.Project;
import com.hamster.crm.pojo.ProjectCategory;
import com.hamster.crm.pojo.ProjectCategoryConfig;
import com.hamster.crm.pojo.ProjectCourse;
import com.hamster.crm.service.ProjectCourseService;
import com.hamster.crm.service.ProjectService;
import com.hamster.crm.util.JsonUtils;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目管理的Controller
 * @author Tarry
 * @create 2019/12/24 14:43
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectCategoryMapper projectCategoryMapper;
    @Autowired
    private ProjectCourseService projectCourseService;
    @Autowired
    private ProjectCourseMapper projectCourseMapper;
    /**
     * 查询项目分类
     * @return List<ProjectCategory>
     */
    @Override
    public List<ProjectCategory> listProjectCategory() {
        QueryWrapper<ProjectCategory> queryWrapper = new QueryWrapper<>();
        return projectCategoryMapper.selectList(queryWrapper);
    }

    /**
     * 添加项目
     * @param project
     */
    @Override
    public void insert(Project project) {
        project.setDel("0");
        project.setInputTime(new Date());
        project.setInputUser("功能待完善");
        projectMapper.insert(project);
    }

    /**
     * 根据id查询项目详情
     * @param id
     * @return
     */
    @Override
    public Project get(String id) {
        return projectMapper.selectById(id);
    }

    /**
     * 删除项目
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<String> ids) {
        for (String id : ids) {
            projectMapper.deleteTag(id);
        }
    }

    /**
     * 修改项目
     * @param project
     */
    @Override
    public void update(Project project) {
        project.setInputUser("功能待完善");
        project.setInputTime(new Date());

        projectMapper.updateById(project);
    }

    /**
     * 条件查询项目列表，分页
     * @param pageSize      每页条数
     * @param currentPage   当前页码
     * @param title         项目名称
     * @return 列表结果
     */
    @Override
    public PageResult<Project> listProjects(@RequestParam Integer pageSize,@RequestParam Integer currentPage, String title) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0").orderByDesc("id");
        if (StringUtils.isNotBlank(title)){
            queryWrapper.like("title","%"+title+"%");
        }
        IPage pageList = projectMapper.selectPage(new Page<Project>(currentPage,pageSize), queryWrapper);
        Long pages = pageList.getPages();
        List<Project> currentList = pageList.getRecords();
        return new PageResult<Project>(pageList.getTotal(),pages.intValue(),currentList);
    }


    /**
     * 手机端需要显示，购买项目时的下拉框
     * @return
     */
    public List<Project> listOptions(){
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0");  //已修改，查询全部字段

        //解析并查询课程
        List<Project> projects = projectMapper.selectList(queryWrapper);

        for (Project project : projects) {
            String course = project.getCourse();
            //如果课程字段为空则 continue
            if (StringUtils.isBlank(course)){
                continue;
            }
            List<String> courseList = JsonUtils.parseList(course, String.class);
            //解释失败，则continue
            if (courseList == null){
                continue;
            }
            List<ProjectCourse> projectCourses = projectCourseService.queryCourse(courseList);
            project.setProjectCourses(projectCourses);
        }

        //返回结果
        return projects;
    }

    /**
     * 根据项目id，显示该项目下的，课程
     * @param id
     * @return
     */
    @Override
    public List<ProjectCourse> listCourse(String id) {
        Project project = projectMapper.selectById(id);
        List<String> ids = JsonUtils.parseList(project.getCourse(), String.class);
        List<ProjectCourse> projectCourseList = new ArrayList<>();

        for (String i : ids) {
            ProjectCourse courseDetail = projectCourseMapper.getCourseDetail(i);
            projectCourseList.add(courseDetail);
        }

        return projectCourseList;
    }

    /**
     * 分公司返利设置的  课程级别的项目 select下拉框
     * @return
     */
    @Override
    public List<Project> getProjectSelect() {
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();

        projectQueryWrapper.select("id","title");
        projectQueryWrapper.eq("del",0);
        projectQueryWrapper.eq("category_id","1");

        return projectMapper.selectList(projectQueryWrapper);
    }

}
